package org.serverct.finalfight;

import com.sun.istack.internal.NotNull;
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
import org.bukkit.Location;
import org.bukkit.OfflinePlayer;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.serverct.finalfight.game.GameManager;
import org.serverct.finalfight.game.hadesgame.EventType;
import org.serverct.finalfight.internal.game.basic.BasicGame;
import org.serverct.finalfight.internal.game.basic.GameState;
import org.serverct.finalfight.stat.IPlayer;
import org.serverct.finalfight.stat.StatManager;

public class PapiHook extends PlaceholderExpansion {
    @NotNull
    public String getAuthor() {
        return "[南外丶仓鼠]";
    }

    @NotNull
    public String getIdentifier() {
        return "finalfight";
    }

    @NotNull
    public String getVersion() {
        return "2.0.0";
    }

    @Override
    public String onRequest(OfflinePlayer off_player, String identifier) {
        if (!(off_player instanceof Player)) {
            return "N/A";
        }
        Player player = (Player) off_player;
        IPlayer stat = StatManager.stats.get(player.getUniqueId());
        switch (identifier) {
            case "health":
                return String.format("%.2f", player.getHealth());
            case "played":
                return String.valueOf(stat.played);
            case "won":
                return String.valueOf(stat.won);
            case "kill":
                return String.valueOf(stat.kill);
            case "death":
                return String.valueOf(stat.death);
            case "kdq":
                return String.format("%.2f", stat.getKDQuation());
            case "winrate":
                return String.format("%.2f", stat.getWinRate());
        }
        if (identifier.startsWith("experience_")) {
            EventType type = EventType.valueOf(identifier.replace("experience_", ""));
            return String.valueOf(StatManager.stats.get(player.getUniqueId()).experience.get(type));
        }
        BasicGame game = GameManager.getGame(player);
        if (game == null) {
            return "N/A";
        }
        boolean isRunning = game.state == GameState.RUNNING;
        switch (identifier) {
            case "playing":
                return String.valueOf(game.players.size());
            case "living":
                return String.valueOf(game.getLiving());
            case "current_border_size":
                return String.format("%.1f", player.getWorld().getWorldBorder().getSize());
            case "init_border_size":
                boolean isNether = player.getWorld().getEnvironment() == World.Environment.NETHER;
                return String.format("%.1f", game.worldManager.initSize * (isNether ? 8 : 1));
            case "border_dist":
                Location loc = player.getLocation();
                double size = player.getWorld().getWorldBorder().getSize();
                double xDist = size / 2 - Math.abs(loc.getX());
                double zDist = size / 2 - Math.abs(loc.getZ());
                return String.format("%.2f", Math.min(xDist, zDist));
            case "next_event_sec":
                return game.state == GameState.RUNNING ? String.valueOf(game.nextevent_sec) : "N/A";
            case "time":
                return isRunning ? String.valueOf(game.timeCounter) : "-1";
            case "state":
                return isRunning ? "§a进行中" : "§c等待中";
            default:
                return identifier;
        }
    }
}
